package com.example.administrator.maizuofilm.cinemadetail;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.example.administrator.maizuofilm.BaseActivity;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.CinemaDetail;
import com.example.administrator.maizuofilm.cinema.view.UrlUtils;
import com.example.administrator.maizuofilm.cinemaschedule.CinemaSchedulingActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class CinemaDetailActivity extends BaseActivity {
    @InjectView(R.id.cinema_detail_back)
    ImageView cinemaDetailBack;
    @InjectView(R.id.imageView2)
    ImageView imageView2;
    @InjectView(R.id.cinema_detail_address)
    TextView cinemaDetailAddress;
    @InjectView(R.id.cinema_detail_goAddress)
    RelativeLayout cinemaDetailGoAddress;
    @InjectView(R.id.imageView3)
    ImageView imageView3;
    @InjectView(R.id.cinema_detail_call)
    TextView cinemaDetailCall;
    @InjectView(R.id.cinema_detail_goPhone)
    RelativeLayout cinemaDetailGoPhone;
    @InjectView(R.id.text1)
    TextView text1;
    @InjectView(R.id.cinima_func_drawmarchine)
    TextView cinimaFuncDrawmarchine;
    @InjectView(R.id.text2)
    TextView text2;
    @InjectView(R.id.cinima_func_eye)
    TextView cinimaFuncEye;
    @InjectView(R.id.text3)
    TextView text3;
    @InjectView(R.id.cinima_func_child)
    TextView cinimaFuncChild;
    @InjectView(R.id.text5)
    TextView text5;
    @InjectView(R.id.cinima_func_park)
    TextView cinimaFuncPark;
    @InjectView(R.id.text4)
    TextView text4;
    @InjectView(R.id.cinima_func_hall)
    TextView cinimaFuncHall;
    @InjectView(R.id.text6)
    TextView text6;
    @InjectView(R.id.cinima_func_privilege)
    TextView cinimaFuncPrivilege;
    private String cname = CinemaSchedulingActivity.cinemaName;
    private String cinemaID = CinemaSchedulingActivity.cinemaId;

    @ViewInject(R.id.cinema_detail_cname)
    private TextView cinemName;
    @ViewInject(R.id.cinema_detail_collection)
    private ImageView filmColleciton;

    @ViewInject(R.id.cinema_detail_address)
    private TextView tv_address;
    @ViewInject(R.id.cinema_detail_call)
    private TextView tv_phone;
    @ViewInject(R.id.cinima_func_drawmarchine)
    private TextView tv_drawMarchine;
    @ViewInject(R.id.cinima_func_eye)
    private TextView tv_eye;
    @ViewInject(R.id.cinima_func_child)
    private TextView tv_child;
    @ViewInject(R.id.cinima_func_park)
    private TextView tv_park;
    @ViewInject(R.id.cinima_func_hall)
    private TextView tv_hall;
    @ViewInject(R.id.cinima_func_privilege)
    private TextView tv_privilege;

    private boolean flag = false;
    private HttpUtils httpUtils;
    private  String  path= UrlUtils.cinemaDetail+cinemaID+UrlUtils.detailLast;

    private String address;
    private String phone;
    private String draw;
    private String gpsAddress;
    private String Deye;
    private String childTicket;
    private String hall;
    private String park;
    private String privilege;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cinema_detail);
        ButterKnife.inject(this);
        ViewUtils.inject(this);
        httpUtils = new HttpUtils();
        initView();
        initData();

    }

    public void initView() {
        cinemName.setText(cname);
    }

    public void initData() {
        httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {

            @Override
            public void onFailure(HttpException exception, String arg1) {
                throw new RuntimeException("网络连接失败");
            }

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String jsonString = responseInfo.result;
                try {
                    CinemaDetail cinemaDetail = JSON.parseObject(new JSONObject(jsonString)
                            .getString("cinemaDetails").toString(), CinemaDetail.class);
                    address = cinemaDetail.getAddress();
                    phone = cinemaDetail.getPhone().get(0).getNo();
                    gpsAddress = cinemaDetail.getGpsAddress();
                    draw = cinemaDetail.getFunInfo().get(0).getInfo();
                    childTicket = cinemaDetail.getFunInfo().get(1).getInfo();
                    park = cinemaDetail.getFunInfo().get(2).getInfo();
                    privilege = cinemaDetail.getFunInfo().get(3).getInfo();
                    Deye = cinemaDetail.getFunInfo().get(4).getInfo();
                    hall = cinemaDetail.getFunInfo().get(5).getInfo();
                    initTextView();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void initTextView() {
        tv_address.setText(address);
        tv_phone.setText(phone);
        if (draw != null && !TextUtils.isEmpty(draw)) {
            tv_drawMarchine.setText(draw);
        } else {
            tv_drawMarchine.setText("暂无相关信息");
        }
        if (Deye != null && !TextUtils.isEmpty(Deye)) {
            tv_eye.setText(Deye);
        } else {
            tv_eye.setText("暂无相关信息");
        }
        if (childTicket != null && !TextUtils.isEmpty(childTicket)) {
            tv_child.setText(childTicket);
        } else {
            tv_child.setText("暂无相关信息");
        }
        if (park != null && !TextUtils.isEmpty(park)) {
            tv_park.setText(park);
        } else {
            tv_park.setText("暂无相关信息");
        }
        if (hall != null && !TextUtils.isEmpty(hall)) {
            tv_hall.setText(hall);
        } else {
            tv_hall.setText("暂无相关信息");
        }
        if (privilege != null && !TextUtils.isEmpty(privilege)) {
            tv_privilege.setText(privilege);
        } else {
            tv_privilege.setText("暂无相关信息");
        }


    }
//
//      @OnClick(R.id.cinema_detail_buy)
//      public  void buyTicket(View view){
//    	   Intent intent=new Intent(CinemaDetailActivity.this,ExchangeTicketActivity.class);
//    	   startActivity(intent);
//    	  CinemaDetailActivity.this.finish();
//      }

    @OnClick(R.id.cinema_detail_goPhone)
    public void goPhone(View view) {
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
        startActivity(intent);
    }

    @OnClick({R.id.cinema_detail_back, R.id.cinema_detail_collection})
    public void clickBack(View view) {
        switch (view.getId()) {
            case R.id.cinema_detail_back:
                this.finish();
                break;
            case R.id.cinema_detail_collection:
                flag = !flag;
                if (flag) {
                    filmColleciton.setBackgroundResource(R.drawable.iv_cinema_collect_org);
                } else {
                    filmColleciton.setBackgroundResource(R.drawable.iv_back_blue);
                }
                break;


        }

    }

}
